/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.angrybyte.numberpicker.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 * ColorStateList
 *
 */
public class ColorStateList {
    private HashMap<int[], Integer> colorStateList = new HashMap<>();

    public ColorStateList(int[][] stateIdentifiers, int[] stateColors) {
        for (int i = 0; i< stateIdentifiers.length; i++) {
            int[] states = stateIdentifiers[i];
            if (states != null && states.length > 0) {
                colorStateList.put(states, stateColors[i]);
            }
        }
    }

    /**
     * add state color
     *
     * @param states the state list
     * @param color the color
     */
    public void addState(int[] states, int color) {
        if (states != null && states.length > 0) {
            colorStateList.put(states, color);
        }
    }

    /**
     * get color by state
     *
     * @param states the state list
     * @param color the default color
     * @return color int
     */
    public int getColorForState(int[] states, int color) {
        if (states != null && states.length > 0) {
            if (colorStateList.get(states) != null) {
                return colorStateList.get(states);
            }
        }
        return color;
    }

    public int[][] getStates() {
        Set<int[]> keys = colorStateList.keySet();
        return (int[][]) keys.toArray();
    }

    public int[] getColors() {
        Collection<Integer> keys = colorStateList.values();
        return Arrays.stream(keys.toArray(new Integer[keys.size()])).mapToInt(Integer::intValue).toArray();
    }
}